package lottery

import (
	"aquaman/app/biz/lottery/model"
	"aquaman/app/biz/m"
	infraDB "aquaman/app/infra/db"
	infraLog "aquaman/app/infra/log"
	"context"
	"strings"
	"time"
)




type Service struct {
	db 				*infraDB.DB
	logger          infraLog.CtxLogger
	repo			*model.Repository
}

func NewService(
	db 		*infraDB.DB,
	logger 	infraLog.CtxLogger,
	repo 	*model.Repository,
	) *Service {
	return &Service{
		db:     db,
		logger: logger,
		repo:	repo,
	}
}

func (s *Service) fromDBModel(origin *model.Lottery,lotteryType model.LotteryType) *m.Lottery {
	var name string
	if lotteryType == model.SSQ {
		name = "双色球"
	}else {
		name = "大乐透"
	}
	return &m.Lottery{
		Type:        int64(lotteryType),
		Name:        name,
		Code:        origin.Code,
		Date:        origin.Date,
		Red:         strings.Split(origin.Red,","),
		Blue:        strings.Split(origin.Blue,","),
		Sales:       int64(origin.Sales),
		PoolMoney:   int64(origin.PoolMoney),
		FirstCount:  int64(origin.FirstCount),
		FirstMoney:  int64(origin.FirstMoney),
		SecondCount: int64(origin.SecondCount),
		SecondMoney: int64(origin.SecondMoney),
		ThirdCount:  int64(origin.ThirdCount),
		ThirdMoney:  int64(origin.ThirdMoney),
	}
}

func (s *Service) GetLastestLottery(ctx context.Context) (list []*m.Lottery) {
	ssq := s.repo.GetLastestLotteryByType(model.SSQ)
	m_ssq := s.fromDBModel(ssq,model.SSQ)
	dlt := s.repo.GetLastestLotteryByType(model.DLT)
	m_dlt := s.fromDBModel(dlt,model.DLT)
	ssqDate, _ := time.Parse("2006-01-02", ssq.Date)
	dltDate, _ := time.Parse("2006-01-02", dlt.Date)
	if ssqDate.Unix() > dltDate.Unix() {
		list = []*m.Lottery{m_ssq,m_dlt}
	}else {
		list = []*m.Lottery{m_dlt,m_ssq}
	}
	return
}

func (s *Service) GetLotteryByType(ctx context.Context,lotteryType int64,page uint64,perPage uint64) (list []*m.Lottery, isEnd bool) {
	var origin []model.Lottery
	origin, isEnd = s.repo.GetLotteryByType(model.LotteryType(lotteryType),page,perPage)
	for _,v := range origin {
		list = append(list,s.fromDBModel(&v,model.LotteryType(lotteryType)))
	}
	return
}

func (s *Service) HandleSSQSpider(ctx context.Context) int64 {
	var (
		basicInterval int64 = 0
		interval      int64 = 60
	)
	spider := NewSpider()
	lottery := spider.FetchSSQ()
	if s.repo.CheckLotteryCode(model.SSQ,lottery.Code) >= 1 {
		s.repo.UpdateLottery(ctx,model.SSQ,lottery)
	}else {
		s.repo.AddLottery(ctx,model.SSQ,lottery)
	}
	s.db.CommitOrAbort(ctx)
	day := time.Now().Weekday()
	hour := time.Now().Hour()
	if (day == time.Sunday || day ==  time.Tuesday || day == time.Thursday) && hour >= 21 {
		return interval
	}

	return basicInterval
}

func (s *Service) HandleDLTSpider(ctx context.Context) int64 {
	var (
		basicInterval int64 = 0
		interval      int64 = 60
	)

	spider := NewSpider()
	lottery := spider.FetchDLT()
	if s.repo.CheckLotteryCode(model.DLT,lottery.Code) >= 1 {
		s.repo.UpdateLottery(ctx,model.DLT,lottery)
	}else {
		s.repo.AddLottery(ctx,model.DLT,lottery)
	}
	s.db.CommitOrAbort(ctx)

	day := time.Now().Weekday()
	hour := time.Now().Hour()
	if (day == time.Monday || day ==  time.Wednesday || day == time.Saturday) && hour >= 20 {
		return interval
	}

	return basicInterval
}

